#!/bin/bash

# 定义路由参数
ROUTE="192.168.0.0/24"
GATEWAY="172.27.0.1"
SERVICE_NAME="add-route.service"
UNIT_FILE_PATH="/etc/systemd/system/${SERVICE_NAME}"

# 创建 systemd 服务单元文件
sudo sh -c "cat > ${UNIT_FILE_PATH} <<EOF
[Unit]
Description=Add static route to network
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c 'ip route add ${ROUTE} via ${GATEWAY}'

[Install]
WantedBy=multi-user.target
EOF"

# 重新加载 Systemd 配置
sudo systemctl daemon-reload

# 启用服务
sudo systemctl enable ${SERVICE_NAME}

# 输出确认信息
echo "The service ${SERVICE_NAME} has been created and enabled. Your route will be added automatically at startup."
